<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/6/20
 * Time: 14:59
 * Mail:87658989@qq.com
 */

namespace app\adminapi\model;

use app\adminapi\filter;
use think\exception\DbException;
use traits\model\SoftDelete;

class RefuelRecord extends Common
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    /**
     * 加油列表
     * @param string $fields
     * @param array $where
     * @param int $page
     * @param int $num
     * @return mixed
     */
    public function listRefuel($fields='*',$where=[],$page=1,$num=10){
        $filter          = new filter\RefuelRecordFilter($fields,$where);
        $condition = $filter->geFiltered();
        $where = $condition['where'];
        $fields = $condition['field'];

        $buildQuery = function($query) use ($where) {
            self::authCompanyQuery($query, $where, 'company_id');
        };

        try{
            $data['data'] = $this->alias('a')
                ->field($fields)
                ->where($buildQuery)
                ->page($page,$num)
                ->select();
        }catch (DbException $e){
            abort(0,$e->getMessage());
        }
        $data['count'] = $this->alias('a')
            ->where($buildQuery)
            ->count();
        $data['data']        = $filter->checkData($data['data']);
        $data['description'] = $filter->title;
        $data['fields']      = $filter->fieldTitle;
        return $data;
    }
}